#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <atomic>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>
namespace ns_util
{
    class TimeUtil
    {
    public:
        // 获取1970年1月1日至今的秒数 --- 时间戳
        static std::string GetTimeStamp()
        {
            struct timeval Tvl;
            gettimeofday(&Tvl, nullptr);
            return std::to_string(Tvl.tv_sec);
        }
        static std::string GetMsLevelTimestamp()
        {
            struct timeval Tvl;
            gettimeofday(&Tvl, nullptr);
            return std::to_string(Tvl.tv_sec * 1000 + Tvl.tv_usec / 1000);
        }
    };

    const std::string temp_path = "../temp/";

    class PathUtil
    {
    public:
        // 形成我们的不同类别临时文件的路径
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string pathname = temp_path;
            pathname += file_name;
            pathname += suffix;
            return pathname;
        }
      
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        // 构建可执行程序的完整路径+后缀名
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }

        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            // stat:获得一个文件的属性  如果文件存在 ---- 属性也存在
            if (stat(path_name.c_str(), &st) == 0)
                return true;
            else
                return false;
        }
        // 形成唯一的文件名
        static std::string UniqFileName()
        {
            static std::atomic_uint id(0);
            id++;
            std::string ms = TimeUtil::GetMsLevelTimestamp();
            std::string uniq_id = std::to_string(id);
            return ms + "_" + uniq_id;
        }

        static bool WriteFile(const std::string &path_name, const std::string &content)
        {
            std::ofstream out(path_name);
            if (!out.is_open())
            {
                return false;
            }
            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string &path_name, std::string *content, bool keep = false)
        {
            content->clear();
            std::ifstream in(path_name);
            if (!in.is_open())
            {
                return false;
            }
            std::string line;
            while (std::getline(in, line))
            {
                *(content) += line;
                *(content) += keep ? "\n" : "";
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SpileString(const std::string &target, std::vector<std::string> *out, const std::string &sep)
        {
            boost::split((*out), target, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}
